<?php
/**
 * 各功能模块的新增方法:AJAX
 */
class AjaxInsertController extends Yaf_Controller_Abstract
{
	const NET_OK = 10;
	const NET_NOAUTH = 20;
	/**
	 * 初始化,禁用自动渲染
     * @method init
	 */
	public function init()
	{
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		Yaf_Dispatcher::getInstance()->disableView(); // 关闭模板
	}
	private function json($data)
	{
		header('Content-type: application/json');
		echo json_encode($data);
	}
	/**
	*新增楼层信息
	*@method addFloorAction
	*/
	public function addFloorAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		$result = net::inst('autoBG')->floorInsert($params);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	/**
	*修改楼层信息
	*@method updateFloorAction
	*/
	public function updateFloorAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		$result = net::inst('autoBG')->floorUpdate($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	/**
	*路点添加
	*@method roadAddAction
	*/
	public function roadAddAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		$result = net::inst('autoBG')->setMagPoint($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	/**
	*路点修改
	*@method roadEditAction
	*/
	public function roadEditAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->magPointUpdate($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}else{
			$response['error'] = '服务器连接错误！';
		}
		$this->json($response);
	 }
	/**
	* 路径添加
	*@method addPathAction
	*/
	public function addPathAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->setPath($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	/**
	*工作区新增
	*@method addWorkAreaAction
	*/
	public function addWorkAreaAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		$result = net::inst('autoBG')->workAreaInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	/**
	*场景新增
	*@method addSceneAction
	*/
	public function addSceneAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->sceneInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }
	 /**
	*地图元素新增
	*@method addMapElemAction
	*/
	public function addMapElemAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->mapElemInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }	
	 /**
	*充电桩新增
	*@method addAgvAction
	*/
	public function addChargerAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->chargerInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }				
	/**
	*agv新增
	*@method addAgvAction
	*/
	public function addAgvAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		$result = net::inst('autoBG')->agvInsert($params);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	 }	
	 /**
	*设备网络地址新增
	*@method addNetAddressAction
	*/
	public function addNetAddressAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->equipAddressInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}else{
			$response['error'] = '服务器连接错误！';
		}
		$this->json($response);
	 }	
	  /**
	*设备型号新增
	*@method addUnitTypeAction
	*/
	public function addUnitTypeAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//echo json_encode($params);
		$result = net::inst('autoBG')->equipTypeInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}else{
			$response['error'] = '服务器连接错误！';
		}
		$this->json($response);
	 }	
	  /**
	*工作区修改
	*@method addWorkAreaAction
	*/
	public function workAreaEditAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$params = $_POST;
		//$result = net::inst('autoBG')->workspaceInsert($params);
		if(isset($result->ret)&&$result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg; 
		}else if(isset($result->ret)&&$result->ret == self::NET_NOAUTH){
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}else{
			$response['error'] = '服务器连接失败！';
		}
		$this->json($response);
	 }	
	/**
	*新增磁导航路点
	*@method addAgvRoadAction
	*/
	public function addAgvRoadAction(){
		
		$this->json($response);
	}
	/**
	*新增料车
	*@method addSkipcarAction
	*/
	public function addSkipcarAction(){
		$params = $_POST;
		$response['status'] = 'succeed';
		$response['info'] = '添加成功！';
		$this->json($response);
	}
	/**
	*新增车辆信息
	*@method insertVehicleAction
	*/
	public function insertVehicleAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/');
			return false;
		}
		$vehicle = $_POST;
		$vehicle["userId"] = $user['id'];
		if(empty($vehicle["vehicleId"])){
			unset($vehicle["vehicleId"]);
			$result = net::inst('northBG')->insertVehicle($vehicle);
		}else{
			$result = net::inst('northBG')->updateVehicle($vehicle);
		}
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*新增到单
	*@method insertArriveRecAction
	*/
	public function insertArriveRecAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$arriveRec = $_POST;
		$arriveRec["userId"] = $user['id'];
		$result = net::inst('northBG')->insertArriveRec($arriveRec);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*新增墊税
	*@method insertTaxAction
	*/
	public function insertTaxAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$tax = $_POST;
		$result = net::inst('northBG')->insertTax($tax);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*新增保税
	*@method insertBondAction
	*/
	public function insertBondAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$bond = $_POST;
		$result = net::inst('northBG')->insertBond($bond);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*新增入库
	*@method insertEnterAction
	*/
	public function insertEnterAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$enter = $_POST;
		$result = net::inst('northBG')->insertEnter($enter);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*放行
	*@method insertReleaseAction
	*/
	public function insertReleaseAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$release = $_POST;
		$release["userId"] = $user["id"];
		$result = net::inst('northBG')->insertRelease($release);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	*发票
	*@method insertInvoiceAction
	*/
	public function insertInvoiceAction(){
		$user = cookie::get('user');
		if(!$user){
			$this->redirect('/index/login/');
			return false;
		}
		$invoice = $_POST;
		$invoice["userId"] = $user["id"];
		$result = net::inst('northBG')->insertInvoice($invoice);
		if($result->ret == self::NET_OK){
			$response['status'] = 'succeed';
			$response['info'] = $result->msg;
		}else{
			$response['status'] = 'fail';
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	 * 管理员新增
     * @method adminInsertAction
	 */
	function userInsertAction(){
		$user = cookie::get('user');
		if (!$user)
		{
			$this->redirect("/index/");
			return;
		}
		$param                  = $this->getRequest();//获取查询参数
		$inParam["userName"]    = trim($param->getRequest('userName'));
		$inParam["trueName"]    = trim($param->getRequest('realName'));
		$inParam["status"]      = $param->getRequest('status');
		$inParam["sex"]         = $param->getRequest('sex');
		$inParam["phone"]       = trim($param->getRequest('phone'));
		$inParam["position"]    = trim($param->getRequest('position'));
		$inParam["email"]       = trim($param->getRequest('email'));
		$inParam["desc"]        = trim($param->getRequest('desc'));
		$inParam["userId"]      = $user["id"];
		$adminId                = $param->getRequest('adminId');
		if(!empty($adminId)){
			$inParam["adminId"] = $adminId;
			$result = net::inst('northBG')->userUpdate($inParam);
		}else{
			$result = net::inst('northBG')->userInsert($inParam);
		}
		if($result->ret == self::NET_OK){
			$response['status'] = "succeed";
			$response['info'] = $result->msg;
		}else{
			$response['status'] = "fail";
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	 * 分配权限右移
     * @method distributAuthRightAction
	 */
	function distributAuthRightAction(){
		$user = cookie::get('user');
		if (!$user)
		{
			$this->redirect("/index/");
			return;
		}
		$param                   = $this->getRequest();//获取查询参数
		$inParam["userId"]       = $user["id"];
		$inParam["adminId"]      = $param->getRequest('adminId');
		$inParam["deptId"]       = $param->getRequest('deptId');
		$nodes                   = explode('*',$param->getRequest('nodes'));
		$list = array();
		for($i=0;$i<count($nodes)-1;$i++){
			$jsonObject = json_decode($nodes[$i]);
			$fun = new stdClass();
			$fun->byDept=$jsonObject->byDept;
			$fun->functionId=$jsonObject->functionId;
			$fun->permit=1;
			$fun->subSystem=$jsonObject->subSystem;
			array_push($list, $fun);
		}
		$inParam["list"] = $list;
		$result = net::inst('northBG')->distributAuthRight($inParam);
		if($result->ret == self::NET_OK){
			$response['status'] = "succeed";
			$response['info'] = $result->msg;
		}else{
			$response['status'] = "fail";
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	 * 分配权限左移
     * @method distributAuthLeftAction
	 */
	function distributAuthLeftAction(){
		$user = cookie::get('user');
		if (!$user)
		{
			$this->redirect("/index/");
			return;
		}
		$param                   = $this->getRequest();//获取查询参数
		$inParam["userId"]       = $user["id"];
		$inParam["adminId"]      = $param->getRequest('adminId');
		$inParam["deptId"]       = $param->getRequest('deptId');
		$nodes                   = explode('*',$param->getRequest('nodes'));
		$list = array();
		for($i=0;$i<count($nodes)-1;$i++){
			$jsonObject = json_decode($nodes[$i]);
			$fun = new stdClass();
			$fun->byDept=$jsonObject->byDept;
			$fun->functionId=$jsonObject->functionId;
			$fun->permit=1;
			$fun->subSystem=$jsonObject->subSystem;
			array_push($list, $fun);
		}
		$inParam["list"] = $list;
		$result = net::inst('northBG')->distributAuthLeft($inParam);
		if($result->ret == self::NET_OK){
			$response['status'] = "succeed";
			$showMsg=$result->msg;
			if(!empty($result->list)&&count($result->list)>0){
				$count = count($result->list);
				$showMsg.='其中';
				for($i=0;$i<$count;$i++){
					$showMsg.=$result->list[$i];
					if($i!=$count-1)
						$showMsg.='';
				}
				$showMsg.='这'.$count.'个权限是全部门权限,因此权限移除后,用户仍具备此'.$count.'个权限,若想移除,选择"全部门权限"进行移除';
			}
			$response["info"] =$showMsg;
		}else{
			$response['status'] = "fail";
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
	/**
	 * 角色新增
     * @method roleInsertAction
	 */
	function roleInsertAction(){
		$user = cookie::get('user');
		if (!$user)
		{
			$this->redirect("/index/");
			return;
		}
		$param                  = $this->getRequest();//获取查询参数
		$inParam["status"]      = $param->getRequest('status');
		$inParam["roleName"]    = trim($param->getRequest('roleName'));
		$inParam["memo"]        = trim($param->getRequest('memo'));
		$inParam["userId"]      = $user["id"];
		$roleId                 = $param->getRequest('roleId');
		if(!empty($roleId)){
			$inParam["roleId"] = $roleId;
			$result = net::inst('northBG')->roleUpdate($inParam);
		}else{
			$result = net::inst('northBG')->roleInsert($inParam);
		}
		if($result->ret == self::NET_OK){
			$response['status'] = "succeed";
			$response['info'] = $result->msg;
		}else{
			$response['status'] = "fail";
			$response['info'] = $result->msg;
		}
		$this->json($response);
	}
}
?>
